package com.juku2024.juku.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexWrap
import com.uu.main.callback.OnNoDoubleItemClickListener
import com.juku2024.juku.R
import com.juku2024.juku.SearchActivity
import com.juku2024.juku.adapter.HistorySearchAdapter
import com.juku2024.juku.adapter.HotSearchAdapter
import com.juku2024.juku.base.BaseFragment
import com.juku2024.juku.callback.OnNoDoubleClickListener
import com.juku2024.juku.tools.WrapFlexLayoutManager
import com.juku2024.juku.tools.WrapLinearLayoutManager
import com.juku2024.juku.viewmodel.SearchViewModel
import com.juku2024.juku.widget.LoadingView

class HotSearchFragment : BaseFragment() {

    private lateinit var ivDelete: View
    private lateinit var historyGroup: View
    private lateinit var searchLoading: LoadingView
    private lateinit var hotRecycler: RecyclerView
    private lateinit var historyRecycler: RecyclerView
    private lateinit var tvHotSearchTitle: TextView

    private val hotAdapter by lazy { HotSearchAdapter(ArrayList()) }
    private val historyAdapter by lazy { HistorySearchAdapter(ArrayList()) }
    private val viewModel by lazy { getViewModel(requireActivity().viewModelStore, SearchViewModel::class.java) }

    override fun layoutView(inflater: LayoutInflater, container: ViewGroup?): View {
        return inflater.inflate(R.layout.fragment_hot_search, container, false)
    }

    override fun initView(rootView: View) {
        super.initView(rootView)
        ivDelete = rootView.findViewById(R.id.ivDelete)
        historyGroup = rootView.findViewById(R.id.historyGroup)
        searchLoading = rootView.findViewById(R.id.searchLoading)
        hotRecycler = rootView.findViewById(R.id.hotRecycler)
        historyRecycler = rootView.findViewById(R.id.historyRecycler)
        tvHotSearchTitle = rootView.findViewById(R.id.tvHotSearchTitle)


        hotRecycler.layoutManager = WrapLinearLayoutManager(requireContext())
        hotRecycler.setHasFixedSize(true)
        hotRecycler.adapter = hotAdapter

        //二级选择
        val manager = object : WrapFlexLayoutManager(requireContext()) {
            override fun canScrollVertically(): Boolean {
                return false
            }
        }
        //设置主轴排列方式
        manager.flexDirection = FlexDirection.ROW
        //设置是否换行
        manager.flexWrap = FlexWrap.WRAP
        historyRecycler.layoutManager = manager
        historyRecycler.adapter = historyAdapter
    }

    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)
        doHttpHotSearch()
    }

    override fun bindEvent() {
        super.bindEvent()
        historyAdapter.setOnItemClickListener(object: OnNoDoubleItemClickListener(){
            override fun onNoDoubleClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
                val item = historyAdapter.data.getOrNull(position)
                if (item != null) {
                    val activity = requireActivity() as SearchActivity
                    activity.search(item)
                }
            }
        })

        hotAdapter.setOnItemClickListener(object: OnNoDoubleItemClickListener(){
            override fun onNoDoubleClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
                (requireActivity() as SearchActivity).search(hotAdapter.data[position].name)
            }
        })

        ivDelete.setOnClickListener(object: OnNoDoubleClickListener{
            override fun onNoDoubleClick(v: View?) {
                super.onNoDoubleClick(v)
                showErrorDialog(
                    "您确认清空历史记录吗？",
                    getString(R.string.confirm), {
                        viewModel.deleteSearchHistory()
                        historyAdapter.setList(ArrayList())
                        historyGroup.visibility = View.GONE
                        hideErrorDialog()
                    }, getString(R.string.cancel), {
                        hideErrorDialog()
                    }
                )
            }
        })
    }

    override fun onResume() {
        super.onResume()
        viewModel.getHistorySearchList({
            if (isFragmentNotActive()) return@getHistorySearchList
            historyGroup.visibility = View.VISIBLE
            historyAdapter.setList(it)
        }, {
            if (isFragmentNotActive()) return@getHistorySearchList
            historyAdapter.setList(ArrayList())
            historyGroup.visibility = View.GONE
        })
    }

    private fun doHttpHotSearch() {
        if (hotAdapter.data.isEmpty()) {
            searchLoading.showLoading()
            viewModel.getHotSearch({
                if (isFragmentNotActive()) return@getHotSearch
                searchLoading.hideLoading()
                if (it.data.isNullOrEmpty()) {
                    searchLoading.showEmptyStatus(getString(R.string.empty_list))
                    return@getHotSearch
                }
                tvHotSearchTitle.visibility = View.VISIBLE

                hotAdapter.setList(it.data)
            }, {
                if (isFragmentNotActive()) return@getHotSearch
                searchLoading.hideLoading()
                searchLoading.showErrorStatus(it, getString(R.string.retry)) {
                    searchLoading.showLoading()
                    searchLoading.postDelayed({
                        doHttpHotSearch()
                    }, 500)
                }
            })
        } else {
            tvHotSearchTitle.visibility = View.VISIBLE
        }
    }


}